{% extends "layout/manage.html" %}
{% load static %}
{% block title %}
    项目管理
{% endblock %}

{% block css %}
    <link rel="stylesheet" href="{% static 'plugin/editor-md/css/editormd.preview.min.css' %}">
    <style>
        .panel-default {
            margin-top: 10px;
        }

        .panel-default .panel-heading {
            display: flex;
            flex-direction: row;
            justify-content: space-between;
        }

        .panel-body {
            padding: 0;
        }

        .title-list {
            border-right: 1px solid #dddddd;
            min-height: 500px;
        }

        .title-list ul {
            padding-left: 15px;
        }

        .title-list ul a {
            display: block;
            padding: 5px 0;
        }

        .content {
            border-left: 1px solid #dddddd;
            min-height: 600px;
            margin-left: -1px;
        }
    </style>
{% endblock %}

{% block content %}
    <div class="container-fluid">
        <div class="panel panel-default">
            <div class="panel-heading">
                <div>
                    <a href="{% url "wiki" project_id=request.tartec.project.id %}"><i class="fa fa-book" aria-hidden="true"></i> wiki文档</a>
                </div>
                <div class="function">
                    <a type="button" class="btn btn-success btn-xs"
                       href="{% url "add_wiki" project_id=request.tartec.project.id %}">
                        <i class="fa fa-plus-circle" aria-hidden="true"></i> 新建</a>
                    {% if wiki_object %}
                        <a type="button" class="btn btn-primary btn-xs"
                           href="{% url "update_wiki" project_id=request.tartec.project.id update_id=wiki_object.id %}">
                            <i class="fa fa-pencil-square-o" aria-hidden="true"></i> 修改</a>
                        <a data-toggle="modal" data-target="#isDelete" type="button"
                           class="btn btn-danger btn-xs"
                           href="">
                            <i class="fa fa-trash" aria-hidden="true"></i> 删除</a>
                        <!-- 弹出是否删除的模态框 -->
                        <div class="modal fade" id="isDelete" tabindex="-1" role="dialog"
                             aria-labelledby="myModalLabel">
                            <div class="modal-dialog" role="document">
                                <div class="modal-content">
                                    <div class="modal-header">
                                        <button type="button" class="close" data-dismiss="modal"
                                                aria-label="Close"><span
                                                aria-hidden="true">&times;</span></button>
                                        <h4 class="modal-title" id="myModalLabel">警告</h4>
                                    </div>
                                    <div class="modal-body">
                                        删除之后不可恢复，是否确定删除
                                    </div>
                                    <div class="modal-footer">
                                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                                        <a href="{% url "delete_wiki" project_id=request.tartec.project.id delete_id=wiki_object.id %}"
                                           type="button" class="btn btn-primary">确定</a>
                                    </div>
                                </div>
                            </div>
                        </div>
                    {% endif %}
                </div>
            </div>

            <div class="panel-body">
                <div class="col-sm-3 title-list">
                    <ul id="catalog">

                    </ul>
                </div>
                <div class="col-sm-9 content">
                    {% if wiki_object %}
                        <div id="previewMarkdown">
                            <textarea>{{ wiki_object.content }}</textarea>
                        </div>
                    {% else %}
                        <div style="text-align: center">
                            <h3 class="">《{{ request.tartec.project.project_title }}》wiki文档</h3>
                            <a href="{% url "add_wiki" project_id=request.tartec.project.id %}">
                                <i class="fa fa-plus-circle" aria-hidden="true"></i> 新建项目</a>
                        </div>
                    {% endif %}
                </div>
            </div>
        </div>
    </div>

{% endblock %}

{% block js %}
    <script src="{% static 'plugin/editor-md/editormd.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/marked.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/prettify.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/raphael.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/underscore.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/sequence-diagram.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/flowchart.min.js' %}"></script>
    <script src="{% static 'plugin/editor-md/lib/jquery.flowchart.min.js' %}"></script>
    <script>
        // wiki 的url
        var WIKI_DETAIL_URL = "{% url 'wiki' project_id=request.tartec.project.id  %}"

        $(() => {
            isCatalog()
            isPreView()
        })

        function isPreView() {
            editormd.markdownToHTML("previewMarkdown", {
                // 过滤器
                // 配置对象中的htmlDecode属性指定了需要保留的HTML标签，这里只保留了style、script和iframe标签。
                htmlDecode: "style,script,iframe",
                // 预览模式下是否对代码块进行高亮显示
                previewCodeHighlight: false,
            })
        }

        function isCatalog() {
            $.ajax({
                url: "{% url "wiki_catalog" project_id=request.tartec.project.id %}",
                type: "JSON",
                method: "GET",
                success: (res) => {
                    if (res.status) {
                        console.log(res)
                        $.each(res.data, (index, item) => {
                            // 在页面添加一个li 标签添加一个id 属性 id_数据库里面的id
                            // 在li标签里面添加一个a 标签，文本信息是数据库里面的title，在添加是href 属性
                            // 在li标签里面再添加一个ul 标签，方便后续的添加li
                            // 拼接完整的wiki_detail 的url，然后给a标签添加上href
                            let href = WIKI_DETAIL_URL + "?wiki_id=" + item.id
                            var li = $("<li>").attr("id", "id_" + item.id).append($("<a>").text(item.title).attr("href", href)).append($("<ul>"))
                            if (!item.parent_id) {
                                // 找到id=catalog的标签，在里面添加上述的li
                                $("#catalog").append(li)
                            } else {
                                // 找到id=id_数据库中的parend_id，在标签下的子标签ul 当中添加ul
                                $("#id_" + item.parent_id).children("ul").append(li)
                            }
                        })
                    } else {
                        alert("初始化目录失败")
                    }
                }
            })
        }
    </script>
{% endblock %}